Ext.namespace("TR.admin.news");

TR.admin.news.news = function(panelId, title, iconClz){

	var optBar = genOptBarByFn(create, del, null, toggle, null, null, null, view);

	var statusCombo = genQueryStatusCombo();
	var queryField = genQueryField();
	var queryBar = ["->", "状态:", statusCombo, "关键词:", queryField, {
		text : "查询",
		iconCls : "query",
		handler : function(){
			newsStore.load();
		}
	}];
	var newsStore = new Ext.data.JsonStore({
		url : "message/find",
		fields : ["id", "title", "category", "keywords", "summary", "content", "preview", "status", "createDate"],
		totalProperty:"total",
		root : "data",
		autoLoad : true,
		listeners : {
			beforeload : function(s){
				s.removeAll();
				Ext.apply(s.baseParams, {
					status : statusCombo.getValue(),
					keyword : queryField.getValue()
				});
			}
		}
	});
	var newsSm = new Ext.grid.RowSelectionModel({
		singleSelect : true
	});
	var newsCm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
		header : "标题",
		dataIndex : "title",
		width : 600
	}, {
		header : "状态",
		dataIndex : "status",
		renderer : renderEnumStatus
	}, {
		header : "创建时间",
		dataIndex : "createDate",
		renderer : renderDate,
        width : 150
	}]);
	var newsGrid = new Ext.grid.GridPanel({
		store : newsStore,
		cm : newsCm,
		sm : newsSm,
		tbar : genToolBar(optBar, queryBar, panelId),
		bbar : genPageBar(newsStore)
	});
	return new Ext.Panel({
		id : panelId,
		title : title,
		iconCls : iconClz,
		closable : true,
		layout : "fit",
		items : [newsGrid]
	});

	function create(){
		window.open(ctx + "/admin/news/create/page", "_blank");
	}
	function del(){
		var rec = newsSm.getSelected();
		if(rec==null){
			showFailMsg("请选择新闻");
			return;
		}
		confirmDelete(function(){
			ajaxRequest({
				url : "admin/message/delete/" + rec.get("id"),
				callback : function(){
					newsStore.load();
				}
			});
		});
	}
	function update(){
		var rec = newsSm.getSelected();
		if(rec==null){
			showFailMsg("请选择新闻");
			return;
		}
		window.open(ctx + "/admin/news/update/" + rec.get("id"), "_blank");
	}
	function toggle(){
		var rec = newsSm.getSelected();
		if(rec==null){
			showFailMsg("请选择新闻");
			return;
		}
		ajaxRequest({
			url : "admin/news/toggleStatus/" + rec.get("id"),
			callback : function(){
				newsStore.load();
			}
		});
	}
	function view() {
        var rec = newsSm.getSelected();
        if(rec==null){
            showFailMsg("请选择新闻");
            return;
        }
        window.open(ctx + "/admin/news/view/" + rec.get("id"), "_blank");
    }
}
registerModule("TR.admin.news.news", TR.admin.news.news);